#!/usr/bin/env bash

# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.

set -euo pipefail

passed=0
total=0

while read -r binary package cwd; do
    echo "--- $binary"
    killall -9 storaged computed materialized &> /dev/null || true
    # Run tests, generating a JSON event stream. The jq in the pipeline below
    # prefixes each event's name field, if present, with the package name, so
    # that we can distinguish tests with the same names in different packages.
    if (cd "$cwd" && "/tests/$binary" -Z unstable-options --format json --report-time) \
        | jq -c --arg package "$package" 'if .name then .name = "\($package)::\(.name)" else . end' \
        | tee -a results.json
    then
        ((++passed))
    else
        echo "^^^ +++"
    fi
    ((++total))
done < /tests/manifest

echo "+++ Status report"
echo "$passed/$total commands passed"
if ((passed != total)); then
    exit 1
fi
